/**
 * Created by johnson on 17/4/27.
 */
import {Injectable} from '@angular/core';
import {Http} from "@angular/http";
import {environment} from "../../environments/environment";
import {ParamsModel} from "../model/params.model";
import {WfToast} from "./toast.utils";
declare var $:any;

@Injectable()
export class HttpUtils {
  private baseUrl:string;

  constructor(private http:Http) {
    console.log("HttpUtils constructor")
    this.baseUrl = environment.backend
  }

  transferGetParam(data:Object):string {
    if (data instanceof Object && data != null && data != undefined) {
      let str = '?';
      for (let key of Object.keys(data)) {
        if (data[key]!=undefined && data[key]!='') {
          str += key + '=' + data[key] + '&';
        }
      }
      str = str.substring(0, str.length - 1);
      return str;
    } else {
      return ''
    }

  }

  get(paramsModel:ParamsModel) {
    this.http.get(this.baseUrl + paramsModel.url + this.transferGetParam(paramsModel.reqestData), paramsModel.options).map(result=>result.json()).subscribe(
      function onNext(resp:any) {
        if (resp.code == 0) {
          paramsModel.succCallback(resp.message,paramsModel.context);
        } else {
          (new WfToast()).msg(resp.message);
          paramsModel.failCallback(resp.message)
        }
      },
      function onError(err:any) {
        paramsModel.errorCallback(err)
        if (err.status == 401){
          paramsModel.tokenExpireCallback()
        }
      },
      function onCompleted() {
        paramsModel.finallyCallback()
      }
    );
  }
  post(paramsModel:ParamsModel) {
    this.http.post(this.baseUrl + paramsModel.url, paramsModel.reqestData, paramsModel.options).map(result=>result.json()).subscribe(
      function onNext(resp:any) {
        if (resp.code == 0) {
          paramsModel.succCallback(resp.message,paramsModel.context);
        } else {
          (new WfToast()).msg(resp.message);
          paramsModel.failCallback(resp.message)
        }
      },
      function onError(err:any) {
        paramsModel.errorCallback(err)
        if (err.status == 401){
          paramsModel.tokenExpireCallback()
        }
      },
      function onCompleted() {
        paramsModel.finallyCallback()
      }
    );
  }
  postFormData(paramsModel:ParamsModel){
    $.ajax({
      url: this.baseUrl + paramsModel.url,
      type: 'POST',
      cache: false,
      data: paramsModel.reqestData,
      headers: paramsModel.getHeaders(),
      processData: false,
      contentType: false
    }).done(function(resp) {
      if (resp.code == 0) {
        paramsModel.succCallback(resp.message,paramsModel.context);
      } else {
        (new WfToast()).msg(resp.message);
        paramsModel.failCallback(resp.message)
      }
    }).fail(function(err) {
      paramsModel.errorCallback(err)
      if (err.status == 401){
        paramsModel.tokenExpireCallback()
      }
    });
  }

  put(paramsModel:ParamsModel) {
    this.http.put(this.baseUrl + paramsModel.url, paramsModel.reqestData, paramsModel.options).map(result=>result.json()).subscribe(
      function onNext(resp:any) {
        if (resp.code == 0) {
          paramsModel.succCallback(resp.message,paramsModel.context);
        } else {
          (new WfToast()).msg(resp.message);
          paramsModel.failCallback(resp.message)
        }
      },
      function onError(err:any) {
        paramsModel.errorCallback(err)
        if (err.status == 401){
          paramsModel.tokenExpireCallback()
        }
      },
      function onCompleted() {
        paramsModel.finallyCallback()
      }
    );
  }
}
